﻿using System.Collections.Generic;
using System.Configuration;
using System.DirectoryServices;
using System.Linq;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.Web.DAL
{
    public class Users
    {
        public static DirectoryEntry Entry { get; set; }

        public static User GetUserByID(int userID)
        {
            using (UserDBDataContext db = Utilities.GenerateUserDataContext())
            {
                var user = from u in db.Users
                           where u.UserID == userID
                           select u;

                return user.SingleOrDefault();
            }
        }

        public static User GetUserByUserName(string username)
        {
            using (UserDBDataContext db = Utilities.GenerateUserDataContext())
            {
                var user = from u in db.Users
                           where u.UserName == username
                           select u;

                return user.SingleOrDefault();
            }
        }

        // TEST
        public static IEnumerable<int> GetManagingUsers(int userID)
        {
            using (ProjectDBDataContext db = Utilities.GenerateProjectDataContext())
            {
                var users = from p in db.Projects
                            join pm in db.ProjectMembers on p.ProjectID equals pm.ProjectID
                            where p.ManagerUserID == userID
                            select pm.UserID;

                return users
                    .Distinct()
                    .ToList();
            }
        }

        //TEST
        public static IEnumerable<string> GetUserRoles(string username)
        {
            var user = Users.GetUserByUserName(username);

            if (user != null)
            {
                using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
                {
                    var role = from r in db.Roles
                               where r.RoleID == user.RoleID
                               select r.Name;
                    return role.ToList();
                }
            }

            return new List<string>();
        }
        
        public static string GetUserEmail(int userID)
        {
            User user = Users.GetUserByID(userID);
            string userName = user.UserName;

            return GetUserEmail(userName);
        }

        public static string GetUserEmail(string userName)
        {
            try
            {
                string ldapPath = "LDAP://sftsrc.com"; // "LDAP://DC=sftsrc,DC=com";

                string adUsername = ConfigurationManager.AppSettings["ActiveDirectoryUsername"];
                string adPassword = ConfigurationManager.AppSettings["ActiveDirectoryPassword"];
                DirectoryEntry entry = new DirectoryEntry(ldapPath, adUsername, adPassword);

                // 'Bind to the native AdsObject to force authentication.
#pragma warning disable 168
                object obj = entry.NativeObject;
#pragma warning restore 168
                DirectorySearcher search = new DirectorySearcher(entry);

                search.Filter = "(SAMAccountName=" + userName.Split('\\').Last() + ")";
                search.PropertiesToLoad.Add("mail");
                SearchResult result = search.FindOne();

                var mail = result.Properties["mail"];
                string address = (string)mail[0];
                return address;
            }
            catch // Authentication Failed
            {
                //TODO: Log
            }

            return string.Empty;
        }
    }
}
